package com.wwdx.utils;

import java.io.File;

/**
 * 清理Maven下载超时文件
 * @author liguoliang
 * @date 2018-10-22 14:53:26
 */
public class CleanMvn {

    public static void main(String[] args){
        findAndDelete(new File("D:/apache-maven-3.5.0/repository"));
        System.exit(0);
    }

    private static boolean findAndDelete(File file){
        if(file.exists() && file.isFile()){
            if(file.getName().endsWith("lastUpdated")){
                deleteFile(file.getParentFile());
                return true;
            }
        } else if(file.isDirectory()){
            File[] files = file.listFiles();
            for(File f : files){
                if(findAndDelete(f)){
                    break;
                }
            }
        }
        return false;
    }

    private static void deleteFile(File file){
        if(file.exists() && file.isFile()){
            print("删除文件:" + file.getAbsolutePath());
            file.delete();
        } else if(file.isDirectory()){
            File[] files = file.listFiles();
            for(File f : files){
                deleteFile(f);
            }
            print("删除文件夹:" + file.getAbsolutePath());
            print("====================================");
            file.delete();
        }
    }

    private static void print(String msg){
        System.out.println(msg);
    }
}